<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use my\Tools;
use think\Cache;
use my\Security;
use my\Local;
use my\Define;
class FrActive extends Controller
{
	//erereqqwqw
	public function driver_register()
	{ 
		return $this->fetch();
	}
	public function recommend()
	{ 
		
		return $this->fetch();
	}
	public function driver_experience()
	{ 
		return $this->fetch();
	}
	public function user_register()
	{
		$uid = input('get.uid');//获取的UID
		$this->assign('uid',$uid);
		return $this->fetch();
	}

	public function getCode(){
        $user_phone = input('get.phone');//获取电话号码
        echo Cache::get($user_phone)['code'];
    }

	public function send_code(){
		$user_phone = input('post.user_phone');
		Cache::set($user_phone,array('code'=>rand(1000, 9999),'phone'=>$user_phone),3600);
		Tools::sendMes($user_phone, Cache::get($user_phone)['code']);
		echo json_encode(array("status" => 1,"msg"=>"ok","data"=>0));
		exit;

	}
	public function send_submit(){
		$user_phone = input('post.user_phone');
		$password = input('post.password');
		$uid = input('post.uid');
		$code = (int)input('post.code');
		if (($code != Cache::get($user_phone)['code']) || !$code) {
			echo json_encode(array("status" => 0, "msg" =>'验证码不正确'));
			exit;
		}
		//查询是否已经存在
		$result = Db::table('xm_user')->where(['user_phone'=>Security::encrypt($user_phone)])->find();
		if(!$result){
			//***新用户,判断密码设置是否规范
			if(!preg_match(Define::PASS_REGEX_M,$password)){
				echo json_encode(array("status" => 0,"msg"=>"密码必须包含数字、字母"));
				exit;
				
			}
			$data['user_pass'] = md5($password);
            //***根据IP获取位置
            /*----------------重新写的根据百度API IP地址---------------*/
            $res = Tools::baiduGetIp();
            if($res['status'] != 0){
                echo json_encode(array("status" => 0,"msg"=>'获取IP地址有疑问,请联系管理员'));
                exit;
            }else{
                $data['register_province']  = $res['register_province'];
                $data['register_city']      = $res['register_city'];
                $data['nation']             = $res['nation'];
            }
            /*$res = Tools::get_ip_dizhi();
            if($res['total']){
            	if($res['total']['region_id'] != 'xx'){
               		$data['register_province'] = Db::name('region')->where('region_code',$res['total']['region_id'])->value('region_id');
            	}
                if($res['total']['city_id'] != 'xx'){
                	$data['register_city'] = Db::name('region')->where('region_code',$res['total']['city_id'])->value('region_id');
            	}
            	$data['nation'] = $res['total']['country'];
                //$data['register_city'] = Db::name('region')->where('region_code',$res['total']['city_id'])->value('region_id');
            }*/
          	$data['user_phone']         = Security::encrypt($user_phone);
          	$data['register_ip']        = Tools::getIp();
            $data['last_login_time']    = time();//第一次注册也相当于登录
            $data['register_time']      = time();//第一次注册也相当于登录
            $data['referee_id']         = $uid;//推荐人uid
            $data['referee_type']       = 2;//用户推荐
            //print_r($data);die;
            $self_uid = Db::table('xm_user')->insertGetId($data);
            Tools::sendMes($user_phone, '',25);
            $old_refer = Db::table('xm_refer')->where('uid',$uid)->find();
			if($old_refer){
				//推广表中改用户的上一级,获取该上级的refer_id,以,逗号分隔
				$old_refer_id = $old_refer['refer_id'];
				Db::table('xm_refer')->insertGetId([
					'uid'           =>$self_uid,
					'fid'           =>$old_refer['fid'],
					'time'          =>time(),
					'province_code' =>$res['province_code'],//$res['total']['region_id'],
					'city_code'     =>$res['city_code'],//$res['total']['city_id'],
					'nation'        =>$res['nation'],//$res['total']['country'],
					'refer_id'      =>$old_refer_id.','.$self_uid,
					'register_ip'   =>$data['register_ip'],
                    'status'=>2
					]);
			}else{
                //根据城市ID查询获取加盟商
                $fid = 0;
                if(!empty($data['register_city'])){
                    $franchiseeInfo = Db::name('franchisee')->where(['city_id'=>$data['register_city'],'is_del'=>1])->field('id')->find();
                    if(!empty($franchiseeInfo)){
                        //$fid = $franchiseeInfo['id'];
                    }
                }
				Db::table('xm_refer')->insertGetId([
					'uid'           =>$self_uid,
					'fid'           =>$fid,
					'time'          =>time(),
                    'province_code' =>$res['province_code'],//$res['total']['region_id'],
                    'city_code'     =>$res['city_code'],//$res['total']['city_id'],
                    'nation'        =>$res['nation'],//$res['total']['country'],
					'refer_id'      =>$self_uid,
					'register_ip'   =>$data['register_ip'],
                    'status'=>2
					]);
			}

		}else{
			$self_uid = Db::table('xm_user')->where(['user_phone'=>Security::encrypt($user_phone)])->value('uid');
			//Db::table('xm_user')->where(['user_phone'=>Security::encrypt($user_phone)])->update(['referee_id'=>$uid,'referee_type'=>2]);
			
		}
		
		$url = url('app/index/download',['uid'=>$self_uid]);
		Cache::rm($user_phone);
    	echo json_encode(array("status" => 1,"msg"=>"ok","data"=>$url));
		exit;
		

	}
	public function is_exist(){
		$user_phone = input('post.user_phone');
		$result = Db::table('xm_user')->where(['user_phone'=>Security::encrypt($user_phone)])->find();
		if($result){
			echo json_encode(array("status" => 1,"msg"=>"该用户已经存在"));
			exit;
		}else{
			echo json_encode(array("status" => 0,"msg"=>"ok"));
			exit;
		}

	}
	public function user_recommend()
	{ 
		return $this->fetch();
	}
	public function user_experience()
	{ 
		return $this->fetch();
	}



}
